def read():
    f = open("data.txt", "r")
    # print(f.read())

    # firstline=f.readline()
    # print(firstline)

    # for line in f:
    #     print(line,end="")
    # f.close()

    # f=f.readlines()
    # print(f)

    print(f.read(3))
    # f.seek(5)
    print(f.read(3))
    print(f.read(1))

def write():
    f=open("write.txt","w")
    f.write("hello world\n")
    f.write("jason pg")
    # f.read()
    f.close()

def readPlus():
    f=open("write.txt","r+")
    f.write("111")
    print(f.read())
    f.close()

def writePlus():
    f=open("write.txt","w+")
    f.write("write plus111")
    f.seek(0)
    print(f.read())
    f.close()

'''
写二进制文件百度
'''
def writeBinary():
    f=open("writeBinary.bin","wb")
    b="I am human 我是一个人".encode("UTF-8")
    list=[]
    for s in b:  #73 32 97 109 32 104 117 109 97 110 32 230 136 145 230 152 175 228 184 128 228 184 170 228 186 186
        print(s,end=" ")
        list.append(s)
    print()
    f.write(bytes(list))
    f.close()

    rf=open("writeBinary.bin","rb")
    b=rf.read()
    print(b)
    rf.close()

    b=b'\xe6\x88\x91\xe6\x98\xaf\xe4\xb8\x80\xe4\xb8\xaa\xe4\xba\xba'
    print(bytes.decode(b,"UTF-8"))

    f=open("writeBinary.bin","rb")
    print(f.read(3))
    f.seek(11)
    db=bytes.decode(f.read(3),"UTF-8")
    print(db)

def wbtest():
    hexstr="49 20 61 6D 20 68 75 6D 61 6E E6 88 91 E6 98 AF E4 B8 80 E4 B8 AA E4 BA BA"
    list=hexstr.split(" ")
    f=open("b.bin", "wb")
    for i in list:
        d=int(i,16)
        print(d)
        f.write(bytes([d]))
    f.close()

def hexTest():
    s="我是一个人"
    code=s.encode("UTF-8")
    print(code)
    for c in code:
        print(c)

if __name__ == '__main__':
    # read()
    # write()
    # readPlus()
    # writePlus()
    # writeBinary()
    wbtest()
    # hexTest()